package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.BaseStudentDao;
import com.itheima.edu.info.manager.dao.OtherStudentDao;
import com.itheima.edu.info.manager.dao.StudentDao;
import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.factory.StudentDaoFactory;

public class StudentService {
   //private OtherStudentDao studentDao = new OtherStudentDao();
    BaseStudentDao studentDao = StudentDaoFactory.getStudentDao();//父类引用指向子类对象.
    //获取学生信息
    public  Student[] findAllStudent() {
        //获取学生信息
        Student[] allStudent = studentDao.findAllStudent();
        //判断是否为null
        boolean flag = false;
        for (int i = 0; i < allStudent.length; i++) {
           // Student stu = allStudent[i];
            if (allStudent[i]!=null) {
                flag = true;
                break;
            }
        }
        if (flag){
            return allStudent;
        }else {
            return null;
        }
    }
//添加传递作用
    public boolean addStudent(Student stu) {
        //将Student对象传递给dao

        return studentDao.addStudent(stu);
    }
//判断学号是否存在
    public boolean isExists(String id) {
        Student[] stu = studentDao.findAllStudent();
        boolean exists = false;
        for (int i = 0; i < stu.length; i++) {
            Student student = stu[i];
            if (student!=null&&student.getId().equals(id)){
                exists = true;
                break;
            }
        }
        return exists;
    }
//传递id给dao删除
    public void deleteStudentById(String delId) {
        studentDao.deleteStudentById(delId);
    }
//传递对象和id给dao修改
    public void updateStudent(String updateId, Student newStu) {
        studentDao.updateStudent(updateId,newStu);
    }
}
